﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using PIT.Labs.SharePoint.Extensions.Helper;

namespace PIT.Labs.SharePoint.Extensions
{
    /// <summary>
    /// Handles the determination of the localized category attribute of a Web Part property.
    /// </summary>
    /// <example>
    /// <code>
    /// <![CDATA[
    /// public const String RESOURCES_FILE = "PIT.Labs.ProjectRepository.WebParts.ListRandomizerWebPart";
    /// [WebBrowsable]
    /// [LocalizedWebDisplayName("ITEM_COUNT", RESOURCES_FILE)]
    /// [LocalizedCategoryAttribute("CONFIGURATION", RESOURCES_FILE)]
    /// [Personalizable(PersonalizationScope.Shared)]
    /// public int ItemsToShowCount
    /// {
    ///     get { return _itemsToShowCount; }
    ///     set
    ///     {
    ///         if (value <= 0)
    ///             throw new Microsoft.SharePoint.WebPartPages.WebPartPageUserException("Value must be greater than zero.");
    ///         _itemsToShowCount = value;
    ///     }
    /// }
    /// ]]>
    /// </code>
    /// </example>
    public sealed class LocalizedCategoryAttribute : CategoryAttribute
    {

        private String _resourceFile = String.Empty;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:PIT.Labs.SharePoint.Extensions.LocalizedCategoryAttribute"/> class using the specified category name.
        /// </summary>
        /// <param name="category">The name of the category.</param>
        /// <param name="resourceFile">The base file name of the language resource file containing a localized string value.
        /// For example, if you have a series of resource files named <c>myresources.en-us.resx</c>, <c>myresources.es-es.resx</c>, <c>myresources.de-de.resx</c>, and so on, the value to pass in this parameter is <c>myresources</c>.
        /// </param>
        public LocalizedCategoryAttribute(String category, String resourceFile)
            : base(category)
        {
            this._resourceFile = resourceFile;
        }

        /// <summary>
        /// Looks up the localized name of the specified category.
        /// </summary>
        /// <param name="value">The identifer for the category to look up. </param>
        /// <returns>
        /// <see cref="String"/><br/>
        /// The localized name of the category, or <b>Nothing</b> if a localized name does not exist.
        /// </returns>
        protected override string GetLocalizedString(string value)
        {
            return SPUtilityHelper.GetLocalizedString(value, this._resourceFile);
        }
    }
}
